/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using VSLangProj80;
using EnvDTE;
using System.IO;
using DaveSexton.DocProject.Wizards.Foundation;
using DaveSexton.DocProject.Wizards;
using System.Text.RegularExpressions;
using System.Xml.XPath;
using System.Xml;
using DaveSexton.DocProject.Sandcastle.Configuration;

namespace DaveSexton.DocProject.Sandcastle
{
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
	public sealed class ImportSettingsWizardStep : WizardStep
	{
		#region Public Properties
		public override string Name
		{
			get
			{
				return Resources.Text.ImportSettingsWizardStepName;
			}
		}

		public override bool CanPreloadUserInterface
		{
			get
			{
				return true;
			}
		}
		#endregion

		#region Private / Protected
		private ImportSettingsControl control;
		private readonly ChoosePresentationWizardStep presentationStep;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ImportSettingsWizardStep" /> class.
		/// </summary>
		public ImportSettingsWizardStep(ChoosePresentationWizardStep presentationStep)
		{
			if (presentationStep == null)
				throw new ArgumentNullException("presentationStep");

			this.presentationStep = presentationStep;
		}
		#endregion

		#region Methods
		protected override object GetUserInterface(WizardController controller, bool preloading)
		{
			if (preloading)
				// this step cannot be preloaded since it depends upon a choice made in a previous step
				return null;

			if (control == null || control.Presentation != presentationStep.Presentation)
			{
				control = new ImportSettingsControl(presentationStep.Presentation);

				control.PresentationChanged += (s, e) =>
					{
						IDocProject importFrom = control.Presentation.ImportFrom;

						WizardStep step = controller.FirstStepLastGroup;
						Wizards.ChooseSourcesStep chooseSourcesStep;

						do
						{
							chooseSourcesStep = step as Wizards.ChooseSourcesStep;
						}
						while (chooseSourcesStep == null && (step = step.NextStep) != null);

						if (importFrom == null)
							chooseSourcesStep.ExternalSources = null;
						else
							chooseSourcesStep.ExternalSources = importFrom.ExternalSources;

					};
			}

			return control;
		}

		protected override void WizardFinished(WizardController controller)
		{
			// NOTE: Do nothing.
			
			// Changes are made directly to the selected Presentation object in memory so that when 
			// the ChoosePresentationWizardStep executes its WizardFinished method, it will already 
			// know of the choices that were made in this wizard step.
		}

		protected override void WizardCanceled(WizardController controller)
		{
			// do nothing
		}
		#endregion
	}
}
